package com.baichang.taoshunda.service.impl;

import com.baichang.framework.excelption.ServiceException;
import com.baichang.taoshunda.dao.DeliveryblancceDao;
import com.baichang.taoshunda.entity.DeliveryClerk;
import com.baichang.taoshunda.service.*;
import com.baichang.taoshunda.utils.ConvertTypeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by ${wushenglu} on 17/09/06.
 */
@Service
public class DeliveryBlanceServiceImpl implements DeliveryBlanceService {
    @Autowired
    private DeliveryblancceDao deliveryblancceDao;
    @Autowired
    private DeliveryClerkService deliveryClerkService;
    @Autowired
    private CashService cashService;
    @Autowired
    private UserBankService userBankService;
    @Autowired
    private ParamsService paramsService;

    /**
     * 查看配送员钱包
     *
     * @param disId 配送员编号
     * @return
     * @throws ServiceException
     */
    @Override
    public Map<String, Object> findDeliveryWallet(Integer disId) throws ServiceException {
        Map<String, Object> map = new HashMap<String, Object>();
        DeliveryClerk deliveryClerk = deliveryClerkService.getDeliveryClerkById(disId);
        map.put("cashTime", paramsService.getParamsValue(29, 27).get("value"));
        //配送员余额
        map.put("balance", deliveryClerk.getBalance());
        //如果有提现记录，放入最后一次提现银行卡信息
        List<Map<String, Object>> cashList = cashService.findCashHistoryByDisId(disId);
        if (cashList != null && cashList.size() > 0) {
            Map<String, Object> cashMap = cashList.get(0);
            Integer bankcardId = ConvertTypeUtils.obj2Integer(cashMap.get("bankcardId"));
            Map<String, Object> userBank = userBankService.getDeliveryBankById(bankcardId);
            if (userBank != null) {
                map.put("bankCard", userBank);
            } else {
                map.put("bankCard", new HashMap<>());
            }
        } else {
            //没有，默认排序第一张
            List<Map<String, Object>> mapList = userBankService.findDeliveryBankCard(disId);
            if (mapList != null && mapList.size() > 0) {
                Map<String, Object> userBank = mapList.get(0);
                map.put("bankCard", userBank);
            } else {
                map.put("bankCard", new HashMap<String, Object>());
            }
        }
        return map;
    }
}
